//
//  CCTableView.m
//  TYEJ
//
//  Created by CC on 2019/12/13.
//  Copyright © 2019 CC. All rights reserved.
//

#import "CCTableView.h"
#import "LcFoundation.h"

@interface CCTableView ()
@property(nonatomic, strong) NSMutableArray* cellAdapters;
@property(nonatomic, strong) NSMutableArray* weakTableViews;
@end

@implementation CCTableView

#pragma mark - CellAdapter
- (void)setCellAdapter:(LcCellAdapter*)cellAdapter forTableView:(UITableView*)tableView {
  LC_CHECK_NOT_NULL(cellAdapter);
  LC_CHECK_NOT_NULL(tableView);
  if (self.cellAdapters == nil) {
    self.cellAdapters = [NSMutableArray array];
  }
  if (self.weakTableViews == nil) {
    self.weakTableViews = [NSMutableArray array];
  }
  LC_CHECK_TRUE(self.cellAdapters.count == self.weakTableViews.count);
  tableView.dataSource = cellAdapter;
  tableView.delegate = self;
  cellAdapter.delegate = self;
  cellAdapter.tableView = tableView;

  [self.cellAdapters removeAllObjects];
  [self.cellAdapters addObject:cellAdapter];
  [self.weakTableViews removeAllObjects];
  [self.weakTableViews addObject:[LcWeakReference weakReferenceWithObject:tableView]];
}

- (void)addCellAdapter:(LcCellAdapter*)cellAdapter forTableView:(UITableView*)tableView {
  LC_CHECK_NOT_NULL(cellAdapter);
  LC_CHECK_NOT_NULL(tableView);
  if (self.cellAdapters == nil) {
    self.cellAdapters = [NSMutableArray array];
  }
  if (self.weakTableViews == nil) {
    self.weakTableViews = [NSMutableArray array];
  }
  LC_CHECK_TRUE(self.cellAdapters.count == self.weakTableViews.count);
  tableView.dataSource = cellAdapter;
  tableView.delegate = self;
  cellAdapter.delegate = self;
  cellAdapter.tableView = tableView;
  [self.cellAdapters addObject:cellAdapter];
  [self.weakTableViews addObject:[LcWeakReference weakReferenceWithObject:tableView]];
}

- (LcCellAdapter*)cellAdapterForTableView:(UITableView*)tableView {
  if (self.weakTableViews.count > 0) {
    int index = -1;
    for (LcWeakReference* weakTableView in self.weakTableViews) {
      ++index;
      if (weakTableView.object == tableView) {
        return self.cellAdapters[index];
      }
    }
    LC_FAIL();
  }
  return nil;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
  LcCellAdapter* cellAdapter = [self cellAdapterForTableView:tableView];
  return [cellAdapter tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  LcCellAdapter* cellAdapter = [self cellAdapterForTableView:tableView];
  [cellAdapter onRowSelectedAtIndex:indexPath.row];
}

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath {
  LcCellAdapter* cellAdapter = [self cellAdapterForTableView:tableView];
  return [[cellAdapter cellDataAtIndex:(int)indexPath.row] clickable];
}

- (void)tableView:(UITableView*)tableView
      willDisplayCell:(UITableViewCell*)cell
    forRowAtIndexPath:(NSIndexPath*)indexPath {
  if ([tableView.tableFooterView isKindOfClass:LcLoadMoreView.class]) {
    LcCellAdapter* cellAdapter = [self cellAdapterForTableView:tableView];
    if (indexPath.row == cellAdapter.allCellDatas.count - 1) {
      LcLoadMoreView* loadMoreView = (LcLoadMoreView*)tableView.tableFooterView;
      [loadMoreView triggerShow];
    }
  }
  LcCellAdapter* cellAdapter = [self cellAdapterForTableView:tableView];
  [cellAdapter tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView*)tableView
    didEndDisplayingCell:(UITableViewCell*)cell
       forRowAtIndexPath:(NSIndexPath*)indexPath {
  LcCellAdapter* cellAdapter = [self cellAdapterForTableView:tableView];
  [cellAdapter tableView:tableView didEndDisplayingCell:cell forRowAtIndexPath:indexPath];
}


#pragma mark - LcNotificationDelegate
- (void)notificationReceived:(NSString*)notification auxiliary:(id)auxiliary {
  // do nothing
}

#pragma mark - LcCellAdapterDelegate
- (void)onLoadMore:(LcCellAdapter*)cellAdapter {
  LC_FAIL();
}

- (void)onSelectionChanged:(NSString*)group cellAdapter:(LcCellAdapter*)cellAdapter {
  // do nothing
}

- (void)onCommitEditing:(NSIndexPath*)indexPath cellAdapter:(LcCellAdapter*)cellAdapter {
  // do nothing
}

@end
